﻿/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using Shuriken.Core.Preferences; 

namespace Shuriken.Core.Preferences
{
    public class ConfigPreferencesBackend: Shuriken.Core.Preferences.IPreferencesBackend
    {
        string config_root = "Shuriken";
        private string preferenceName;
        private XmlPropertiesBackend backend; 

        public ConfigPreferencesBackend(string prefName)
        {
            preferenceName = prefName;
        }

        public string MakeKeyPath(string key)
        {
            return string.Format("{0}.{1}.{2}", config_root, preferenceName, key);
        }

        #region IPreferencesBackend Members

        public bool TryGet<T>(string key, out T value)
        {
            bool result = true;
            value = default(T);

            CheckIfBackendLoaded();

            string k = MakeKeyPath(key);

            try
            {
                result = backend.TryGet<T>(k, out value); 
            }
            catch (Exception excp)
            {
                string msg = excp.Message;
                result = false;
            }

            return result;
        }

        public bool TrySet<T>(string key, T value)
        {
            bool result = true;
            string keyPath = MakeKeyPath(key);

            CheckIfBackendLoaded();

            string k = MakeKeyPath(key);

            try
            {
                backend.TrySet<T>(k, value);
                backend.Save(ConfigurationFileName);
            }
            catch (Exception excp)
            {
                string msg = excp.Message;
                result = false;
            }

            return result;
        }

        private void CheckIfBackendLoaded()
        {
            if (backend == null)
            {
                backend = XmlPropertiesBackend.Load(ConfigurationFileName);
            }

            if (backend == null)
            {
                backend = new XmlPropertiesBackend();
            }
        }

        #endregion

        private string ConfigurationFileName
        {
            get { return ApplicationEnvironment.LocalApplicationDataDirectory + "\\props.xml";  }
        }
    }
}
